﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsCalculate.OnlyCalculate
{
    public partial class OnlyCalculate : Form
    {
        public OnlyCalculate()
        {
            InitializeComponent();
            //循环查找所有日志，有则打开
            for (int i = 0; i < 300; i++)
            {
                var curFileName = DateTime.Now.AddDays(-i).ToString("yyyyMMdd");
                if (File.Exists(curFileName))
                {
                    this.richTextBox1.Text = File.ReadAllText($"{curFileName}") + "\n";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var curFormat = this.richTextBox1.Text.Split('\n')
                .ToList().Where(data => data != null&& data !=""&&!data.StartsWith("--") && !data.Contains("结果"));

            foreach (var data in curFormat)
            {
                this.richTextBox1.Text = this.richTextBox1.Text.TrimEnd() + "\n结果 " + new DataTable().Compute(data, null) + "\n";
            }
            //格式化结果
            var textList = this.richTextBox1.Text.Split('\n')
                .ToList();
            var endString = "";
            foreach (var data in textList)
            {
                endString += ((data != null && data != "" && !data.StartsWith("--") && !data.Contains("结果")) ? ("--" + data) : data) + "\n";
            }
            this.richTextBox1.Text = endString;
            File.WriteAllText($"{DateTime.Now.ToString("yyyyMMdd")}", this.richTextBox1.Text);
        }
    }
}
